<?php
/**
 * regd by PhpStorm.
 * User: lee
 */

namespace Biz\validate\activity;

use Biz\model\activity\OutCouponConfig as mOutCouponConfig;
use Biz\module\Classes\OutCoupon as clsOutCoupon;
use Biz\model\activity\CouponConfig as mCouponConfig;


class OutCoupon extends \Doraemon\pockets\validate\Base
{

    public static function rules()
    {
        $supportAssumeList = array_keys(mOutCouponConfig::$supportAssumeDesc);

        $peopleTypeList = array_keys(mOutCouponConfig::$peopleTypeDesc);

        return [
            ['title', 'required', null, '请输入活动页面名称', ['create','update']],
            ['title', 'length', '1,30', '活动页面名称最大30个字符', ['create','update']],

            ['background_color', 'required', null, '活动背景色必填项', ['create','update']],

            ['support_assume', 'InList', ['list' => $supportAssumeList], '优惠券类型错误', ['create','update']],

            ['start_time', 'required', null, '请正确输入开始时间', ['create','update']],
            ['start_time', 'callback', ['callback' => [new self(), 'checkTimeValid']], '开始时间必须大于当前时间并且小于结束时间', ['create','update']],


            ['coupon_id_list', 'required', null, '请正确填写优惠券ID', ['create','update']],
            ['coupon_id_list', 'callback', ['callback' => [new self(), 'checkCouponIdList']], '请正确填写优惠券列表', ['create','update']],

            ['people_type', 'InList', ['list' => $peopleTypeList], '请正确选择领取人群', ['create','update']],

            ['pic_url', 'required', null, '请上传图片', ['create_pic','update_pic']],
            ['weight', 'required', null, '请填写权重', ['create_pic','update_pic']],
            ['weight', 'number', null, '权重为数字', ['create_pic','update_pic']],


            ['btnname', 'required', null, '请填写按钮文字', ['create_mobile','update_mobile']],
            ['backgroundColor', 'required', null, '请正确填写背景色', ['create_mobile','update_mobile']],
            ['backgroundColor', 'length', '7,7', '背景色颜色值必须为7个字符', ['create_mobile','update_mobile']],
            ['weight', 'required', null, '请填写权重', ['create_mobile','update_mobile']],
            ['weight', 'number', null, '权重为数字', ['create_mobile','update_mobile']],

        ];

    }

    public function checkCouponIdList($value, $valueIdentifier, $allData, $obj)
    {

        $couponIdList = array_filter(explode(',', $value));


        try {
            if (empty($couponIdList)) {
                return false;
            }

            $couponList = mCouponConfig::getCouponList(['coupon_id' => $couponIdList,'assume'=>$allData['support_assume'], 'status' => mCouponConfig::STATUS_CAN_RECEIVE]);

            if (empty($couponList)) {
                $obj->setMessageTemplate('请正确填写优惠券ID，或填写的优惠券Id不是'.mOutCouponConfig::$supportAssumeDesc[$allData['support_assume']]);

                return false;
            }

            $idDiffList = array_diff($couponIdList, array_column($couponList, 'coupon_id'));

            if (!empty($idDiffList)) {
                $msgList = array_map(function ($v) use($allData) {
                    return sprintf("优惠券id %d 不存在，或不是".mOutCouponConfig::$supportAssumeDesc[$allData['support_assume']], $v);
                }, $idDiffList);
            } else {
                $isBadList = clsOutCoupon::checkCouponIdListIsBadList($allData['stop_time'], $couponIdList);

                if (!empty($isBadList)) {
                    $msgList = array_map(function ($v) {
                        return sprintf("优惠券id %d 结束时间早于活动结束时间，请修改优惠券或活动时间", $v);
                    }, $isBadList);
                }
            }

            if (!empty($msgList)) {

                $obj->setMessageTemplate(implode('<br>', $msgList));

                return false;
            }


        } catch (\Exception $e) {
            $obj->setMessageTemplate($e->getMessage());
            return false;
        }


        return true;


    }

    public function checkTimeValid($value, $valueIdentifier, $allData)
    {

        $nowTime = strtotime(date('Y-m-d'));

        if (strtotime($value) < $nowTime) {
            return false;
        }

        if (strtotime($value) > strtotime($allData['stop_time'])) {
            return false;
        }

        return true;
    }

}